<template>
    用户名：<input type="text" v-model="username">
    密码：<input type="password" name="" id="" v-model="password">
    <el-button type="primary" @click="login">登录</el-button>
</template>
<script>
export default {
    data() {
        return {
            username: '',
            password: ''
        }
    },
    methods: {
        login() {
            fetch('http://localhost:8082/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: this.username,
                    password: this.password
                })
            }).then(res => res.json())
            .then(data => {
                console.log(data);
                if (data.code == 1) {
                    this.$store.commit('setLoginUser', data.data);
                    sessionStorage.setItem('loginUser', JSON.stringify(data.data));
                    this.$router.push('/index');
                }
                
            })
            .catch(err => console.log(err));
        }
    }

}
</script>